#include "MultiMediaRegistry.h"
#include <iterator>
#include <fstream>
#include <algorithm>
#include <sstream>

bool sort_pred_mm( MultiMedia *one, MultiMedia *other );
bool sort_pred_b( MultiMedia *one, MultiMedia *other );


MultiMediaRegistry::MultiMediaRegistry(void)
{
}


MultiMediaRegistry::~MultiMediaRegistry(void)
{
}

bool sort_pred_mm( MultiMedia *one, MultiMedia *other )
{
	return one->GetTitle().compare( other->GetTitle() ) > 0;
	
	//(one->GetType() < other->GetType() );
}

bool sort_pred_b( MultiMedia *one, MultiMedia *other )
{
	Book *b1, *b2;

	b1 = dynamic_cast<Book*>(one);
	b2 = dynamic_cast<Book*>(other);

	if( (b1->GetAuthor() < b2->GetAuthor()) )
		return true;
	else if( ( (b1->GetAuthor() == b2->GetAuthor()) ) && (b1->GetYear() < b2->GetYear()) )
		return true;
	else if( ( ((b1->GetAuthor() == b2->GetAuthor()) ) && (b1->GetYear() == b2->GetYear()) ) && (b1->GetGenre() < b2->GetGenre()) )
		return true;
	else if( ( (((b1->GetAuthor() == b2->GetAuthor()) ) && (b1->GetYear() == b2->GetYear()) ) && (b1->GetGenre() == b2->GetGenre()) ) &&  (b1->GetTitle() < b2->GetTitle()) )
		return true;
	else if( ( ((((b1->GetAuthor() == b2->GetAuthor()) ) && (b1->GetYear() == b2->GetYear()) ) && (b1->GetGenre() == b2->GetGenre()) ) &&  (b1->GetTitle() == b2->GetTitle()) ) && (b1->GetPages() > b2->GetPages()) )
		return true;
	else
		return false;
}

void MultiMediaRegistry::addEntry( MultiMedia *multiMedia )
{
	registry.push_back( multiMedia );
}

void MultiMediaRegistry::deleteEntry( MultiMedia *multiMedia )
{
	for( auto iter=this->registry.begin(); iter != registry.end(); iter++ )
	{
		if( *iter == multiMedia )
		{
			registry.erase( iter );
			break;
		}
	}
}


void MultiMediaRegistry::writeRegistryToFile()
{
	ofstream registryFile;

	registryFile.open("savefile.txt");

	for( int i = 0; i < registry.size(); i++ )
	{
		(*registry[i]).Print( registryFile );
	}

	registryFile.close();
}

void MultiMediaRegistry::readRegistryFromFile()
{
	ifstream registryFile;
	string strRow;

	registryFile.open( "savefile.txt" );

	while( getline( registryFile, strRow ) )
	{
		istringstream iss( strRow );
		string str;
		char c;

		iss >> c;
		getline( iss, str, ',' );
		if( str.compare("Book") == 0 )
		{
			Book tempBook;
			iss >> tempBook;
			registry.push_back( new Book( tempBook ) );
		}


	}

	registryFile.close();
}

void MultiMediaRegistry::sortRegistry()
{
	std::sort( registry.begin(), registry.end(), sort_pred_mm );

	//std::sort( registry.begin(), registry.end(), sort_pred_b );

	//std::sort( registry.begin(), registry.end() );

	//std::sort( MultiMediaRegistry::TypeCheck()+1, MultiMediaRegistry::TypeCheck(), Movie::sort_pred );

}

void MultiMediaRegistry::searchMultiMediaString( std::vector<MultiMedia *> &vector, std::string type, std::string value )
{
	vector.clear();

	if( type == "Type" )
	{
		int i = 0;
		for( auto iter : registry )
		{
			if( (*iter).GetType() == value )
				vector.push_back(iter);
			i++;
		}
	}
	else if( type == "Title" )
	{
		int i = 0;
		for( auto iter : registry )
		{
			if( (*iter).GetTitle() == value )
				vector.push_back(iter);
			i++;
		}
	}
	else if( type == "Author" )
	{
		int i = 0;
		for( auto iter : registry )
		{
			if( (*iter).GetAuthor() == value )
				vector.push_back(iter);
			i++;
		}
	}
	else if( type == "Genre" )
	{
		int i = 0;
		for( auto iter : registry )
		{
			if( (*iter).GetGenre() == value )
				vector.push_back(iter);
			i++;
		}
	}
}

void MultiMediaRegistry::searchMultiMediaYear( std::vector<MultiMedia *> &vector, int value )
{
	vector.clear();
	int i = 0;
	for( auto iter : registry )
	{
		if( (*iter).GetYear() == value )
			vector.push_back(iter);
		i++;
	}
	
}



std::vector<MultiMedia *>::iterator MultiMediaRegistry::TypeCheck()
{
	std::string type1;

	type1 = (*registry.begin())->GetType();
	for( auto iter = registry.begin(); iter != registry.end(); iter++ )
	{
		if( (*iter)->GetType() != type1 )
		{
			return iter;
		}
	}
}